package org.gruposp2p.proyectokarma.gwt.client.widget;

import org.gruposp2p.proyectokarma.gwt.client.MainEntryPoint;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class ExpandPanelLabel extends Composite {
	
    Anchor anchor;
    Image image;
    boolean isExpanded = false;    
    
    private Handler handler;

	public ExpandPanelLabel () {
		image = new Image(MainEntryPoint.INSTANCE.resources.editAdd());
		HorizontalPanel widget = new HorizontalPanel();
        initWidget(widget);
		image.addClickHandler(new LabelClickHandler());
		image.getElement().getStyle().setMarginTop(3, Unit.PX);
		widget.add(image);
		//image.setStylePrimaryName("iconTag");
		anchor = new Anchor();
		anchor.addClickHandler(new LabelClickHandler());
        Style linkStyle = (Style) anchor.getElement().getStyle();
        linkStyle.setFontSize(15, Unit.PX);
        linkStyle.setMarginLeft(3, Unit.PX);
        widget.getElement().getStyle().setCursor(Style.Cursor.POINTER);
		widget.setStylePrimaryName("expandPanelLabel");
		widget.add(anchor);
	}

	public void setLabelData (String anchorText, Handler handler) {
		this.handler = handler;
		anchor.setText(anchorText);
	}
	
    private class LabelClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if (isExpanded)
				image.setResource(MainEntryPoint.INSTANCE.resources.editAdd());
			else 
				image.setResource(MainEntryPoint.INSTANCE.resources.editRemove());
			isExpanded = !isExpanded;
			handler.expandPanel(isExpanded);
		}	
	}
    
    public interface Handler {
    	void expandPanel(boolean isExpanded);
    }

}
